package com.ccjk.beusoft.thirdlib.plugin2.hook.proxy;


import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import com.ccjk.beusoft.thirdlib.plugin2.PluginManager2;

import java.lang.reflect.Field;

/**
 * Created by CCJK on 2017/12/22.
 * ActivityThread中 mH 的设置的callback,可以先一部处理
 */

public class HCallback implements Handler.Callback {
    private Handler mH;

    public HCallback(Handler mH) {
        this.mH = mH;
    }


    private void before(Message message) {
        try {
            Object activityClientRecord = message.obj;
            Field intentField = activityClientRecord.getClass().getDeclaredField("intent");
            intentField.setAccessible(true);
            Intent intent = (Intent) intentField.get(activityClientRecord);
            if (PluginManager2.get().isPlugin(intent.getPackage())) {
                Intent realIntent = intent.getParcelableExtra(PluginManager2.KEY_PLUGIN_INTENT);
                intentField.set(activityClientRecord, realIntent);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean handleMessage(Message msg) {
        before(msg);
        mH.handleMessage(msg);
        return true;
    }
}
